package com.bulldog.spider.console;

import com.bulldog.spider.ListDepartmentRepo;
import com.bulldog.spider.SiteConst;
import lombok.extern.java.Log;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

@Log
public class ConsoleDepartListPipeLine implements Pipeline {

    @Override
    public void process(ResultItems resultItems, Task task) {
        log.info("捕获页面");
        log.info(resultItems.getRequest().getUrl());
        Iterator iterator = resultItems.getAll().entrySet().iterator();

        while(iterator.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry)iterator.next();
            Object object = entry.getValue();

            // 科室名
            List<String> departNameList = ((ListDepartmentRepo)object).departNameList.stream().toList();
            departNameList.stream().forEach(System.out::println);

            // 链接
            List<String> departHrefList = ((ListDepartmentRepo)object).departHrefList.stream().toList();
            ListIterator<String> lit = departHrefList.listIterator();
            while (lit.hasNext()) {
                System.out.println(SiteConst.DP_HOST + lit.next());
            }

        }

    }
}